<?php

if (!defined('IN_IA')) {
    exit('Access Denied');
}

class Commission_MxShopPage extends WebPage {

    public function main() {

        global $_W,$_GPC;

        $uniacid = $_W['platformid'];

        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;

        $condition = '';

        $paras = array();

        if (empty($starttime) || empty($endtime))
        {
            $starttime = strtotime('-1 month');
            $endtime = time();
        }
        $searchtime = trim($_GPC['searchtime']);
        if (!(empty($searchtime)) && is_array($_GPC['time']) && in_array($searchtime, array('create')))
        {
            $starttime = strtotime($_GPC['time']['start']);
            $endtime = strtotime($_GPC['time']['end']);
            $condition .= ' AND c.' . $searchtime . 'time >= :starttime AND c.' . $searchtime . 'time <= :endtime ';
            $paras[':starttime'] = $starttime;
            $paras[':endtime'] = $endtime;
        }

        if (!(empty($_GPC['searchfield'])) && !(empty($_GPC['keyword'])))
        {
            $paras[':keyword'] = htmlspecialchars_decode($_GPC['keyword'], ENT_QUOTES);
            if ($_GPC['searchfield'] == 'member'){
                $condition .= ' AND (locate(:keyword,m.realname)>0 or locate(:keyword,m.mobile)>0';

            }else if ($_GPC['searchfield'] == 'mendian'){
                $condition .= ' AND locate(:keyword,d.mendianname)>0 ';
            }
        }

        $list = pdo_fetchall('SELECT c.*, m.id as mid, m.realname as realname, m.avatar as avatar, m.mobile as mobile, d.id as mendianid, d.mendianname as mendianname, d.thumb as thumb FROM ' . tablename('mx_shop_service_commission') . ' c LEFT JOIN ' .tablename('mx_shop_user') . ' m ON c.uid=m.id LEFT JOIN '.tablename('mx_shop_mendian').' d ON c.mendianid=d.id WHERE 1 ' . $condition . ' ORDER BY c.id DESC LIMIT '. (($pindex - 1) * $psize) . ',' . $psize, $paras);

        $total = pdo_fetchcolumn('SELECT count(c.id) FROM ' . tablename('mx_shop_service_commission') . ' c LEFT JOIN ' .tablename('mx_shop_user') . ' m ON c.uid=m.id LEFT JOIN '.tablename('mx_shop_mendian').' d ON c.mendianid=d.id WHERE 1 ' . $condition, $paras);

        $reserved = pdo_fetch('SELECT * FROM '.tablename('mx_shop_service_commission_reserved').' WHERE 1 LIMIT 1');
        $pager = pagination($total, $pindex, $psize);

        include $this->template();

    }

    public function commlog() {

        global $_W,$_GPC;

        $uniacid = $_W['platformid'];

        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;

        $id = intval($_GPC['id']);
        if(empty($id)){
            $this->message('参数有误！');
        }
        $condition = ' c.comid=:id';

        $paras = array(':id'=>$id);

        if (empty($starttime) || empty($endtime))
        {
            $starttime = strtotime('-1 month');
            $endtime = time();
        }
        $searchtime = trim($_GPC['searchtime']);
        if (!(empty($searchtime)) && is_array($_GPC['time']) && in_array($searchtime, array('create')))
        {
            $starttime = strtotime($_GPC['time']['start']);
            $endtime = strtotime($_GPC['time']['end']);
            $condition .= ' AND c.' . $searchtime . 'time >= :starttime AND c.' . $searchtime . 'time <= :endtime ';
            $paras[':starttime'] = $starttime;
            $paras[':endtime'] = $endtime;
        }

        if (!(empty($_GPC['searchfield'])) && !(empty($_GPC['keyword'])))
        {
            $paras[':keyword'] = htmlspecialchars_decode($_GPC['keyword'], ENT_QUOTES);
            if ($_GPC['searchfield'] == 'ordersn'){
                $condition .= ' AND locate(:keyword,o.ordersn)>0 ';
            }
        }

        $list = pdo_fetchall('SELECT c.*, o.ordersn as ordersn, o.realprice as realprice, m.id as mid, m.realname as realname, m.avatar as avatar, m.mobile as mobile FROM ' . tablename('mx_shop_service_commission_log') . ' c LEFT JOIN ' .tablename('mx_shop_service_order') . ' o ON c.orderid=o.id LEFT JOIN '.tablename('mx_shop_user').' m ON o.uid=m.id WHERE ' . $condition . ' ORDER BY c.id DESC LIMIT '. (($pindex - 1) * $psize) . ',' . $psize, $paras);

        $total = pdo_fetchcolumn('SELECT count(c.id) FROM ' . tablename('mx_shop_service_commission_log') . ' c LEFT JOIN ' .tablename('mx_shop_service_order') . ' o ON c.orderid=o.id WHERE '  . $condition, $paras);

        $commission = pdo_fetch('SELECT * FROM '.tablename('mx_shop_service_commission').' WHERE id=:id LIMIT 1', array(':id'=>$id));
        if(empty($commission['mendianid'])){
            $member = pdo_fetch('SELECT realname FROM '.tablename('mx_shop_user').' WHERE id=:id LIMIT 1', array(':id'=>$commission['uid']));
            $name = $member['realname'];
        }else{
            $mendian = pdo_fetch('SELECT mendianname FROM '.tablename('mx_shop_mendian').' WHERE id=:id LIMIT 1', array(':id'=>$commission['mendianid']));
            $name = $mendian['mendianname'];
        }
        $level = array('1'=>'店主', '2'=>'创业者', '3'=>'渠道商', '4'=>'省级运营商', '5'=>'区域运营商', '6'=>'推荐人', '7'=>'消费者');

        $pager = pagination($total, $pindex, $psize);

        include $this->template();

    }



}
